$(function () {
  // var fullscree = $("#myFullScree");
  // if(!fullscree.length){
  //   $('body').append('<div id="myFullScree">全屏</div>')
  // }
  // $("#myFullScree").off('click').on('click',function(){
  //   launchFullscreen()
  // })

  $(window).resize(function () {
    setWindow();
  });
});
//获取主题
function getClassList() {
  return ajaxRequest({
    type: "GET",
    url: "/open/exam/getDictValueList?key=yw_task_type",
    success: function (res) {
      const _list = res.data || [];
      if (_list.length) {
        var htm = "";
        _list.forEach(function (v, i) {
          htm =
            '<div class="classItem" onclick="linkToType(this)" data-id="' +
            v.dictValue +
            '">' +
            (v.dictLabel || "") +
            "</div>";
          $("#classList").append(htm);
        });
        bindTypeEven();
      } else {
        // pageJump("/html/question.html");
      }
      setWindow();
    },
  });
}
function linkToType(obj) {
  var key = $(obj).data("id");
  location.href = "/html/questionType.html?key=" + key;
}
//返回首页
function backHome() {
  location.href = "/html/index.html";
}
//全屏
function launchFullscreen() {
  // 检查浏览器是否支持Fullscreen API
  if (
    document.fullscreenEnabled ||
    document.webkitFullscreenEnabled ||
    document.mozFullScreenEnabled ||
    document.msFullscreenEnabled
  ) {
    // 当页面加载完成时，将网页全屏显示
    var elem = document.documentElement;
    if (elem.requestFullscreen) {
      elem.requestFullscreen();
    } else if (elem.webkitRequestFullscreen) {
      elem.webkitRequestFullscreen();
    } else if (elem.mozRequestFullScreen) {
      elem.mozRequestFullScreen();
    } else if (elem.msRequestFullscreen) {
      elem.msRequestFullscreen();
    }
  }
}
///open/exam/getExamList0
//open/exam/getAnswerDetail   get tdId
//open/exam/startExam  post examId
//open/exam/submitExam post {answer:[{id:1,v:1}],tdId:1,name:'',phone:''}
function setWindow() {
  var w_height = window.innerHeight;
  var d_height = $(".comModal").height();
  if (d_height + 100 > w_height) {
    $(".comModal").removeClass("comModalCenter");
  } else {
    $(".comModal").addClass("comModalCenter");
  }
}

function clearWindow() {
  delete window.examId;
  delete window.tdId;
  delete window.questionInd;
  delete window.questionList;
}
function getQuestions(id) {
  var t = $.Deferred();
  ajaxRequest({
    type: "POST",
    url: "/open/exam/startExam?examId=" + id,
    success: function (res) {
      const obj = res.data || {};
      //缓存数据
      t.resolve(obj);
    },
    error: function (error) {
      t.reject(error);
    },
  });
  return t.promise();
}
function getTypeList(key) {
  return ajaxRequest({
    type: "GET",
    url: "/open/exam/getExamList?examType=" + key,
    success: function (res) {
      const _list = res.data || [];
      if (_list.length) {
        var htm = "";
        _list.forEach(function (v, i) {
          htm =
            '<div class="typeItem" data-id="' +
            v.examId +
            '">' +
            '<span class="number">' +
            (i + 1) +
            "</span>" +
            '<span class="text">' +
            (v.examTitle || "") +
            "</span>" +
            '<span class="link">点击进入</span>' +
            "</div>";
          $("#typeList").append(htm);
        });
        bindTypeEven();
      } else {
        // pageJump("/html/question.html");
      }
      setWindow();
    },
  });
}

function bindTypeEven() {
  $("#questionType")
    .off("click", ".typeItem")
    .on("click", ".typeItem", function () {
      var id = $(this).data("id");
      location.href = "/html/question.html?id=" + id;
    });
}
function pageJump(url) {
  location.href = url;
}
//ajax
function ajaxRequest(settings) {
  // 参数对象
  var baseUrl = "/topic/api";
  var params = settings.params === undefined ? null : settings.params;
  var dataType = settings.dataType === undefined ? "json" : settings.dataType;
  var type = settings.type === undefined ? "POST" : settings.type;
  var timeout = settings.timeout === undefined ? 60 * 1000 : settings.timeout;
  if (settings.url.indexOf("?") > 0) {
    settings.url += "&r=" + Math.random();
  } else {
    settings.url += "?r=" + Math.random();
  }
  return $.ajax({
    // contentType: "application/x-www-form-urlencoded", //application/json
    url: baseUrl + settings.url,
    type: type,
    data: params === null ? undefined : params,
    async: true,
    dataType: dataType,
    cache: false,
    timeout: timeout,
    success: function (result, textStatus) {
      var code = result.code;
      var msg = result.msg;
      if (code == 500) {
        alert(msg);
      } else if (code === 0) {
        if (settings.success) {
          // 回调方法
          settings.success(result);
        } else {
          console.log(result.msg);
        }
      }
    },
    error: function (xhr, status, error) {
      alert(xhr.responseText);
      if (settings.error) {
        // 回调方法
        settings.error(xhr);
      } else {
        console.log(xhr.responseText);
      }
    },
    complete: function (xhr, status) {},
  });
}
